package com.lee.demo.util;

import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lee.demo.R;
import com.lee.demo.base.BaseActivity;
import com.lee.demo.model.GridListBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.RequiresApi;
import cn.lee.cplibrary.util.LogUtil;
import cn.lee.cplibrary.util.ScreenUtil;
import cn.lee.cplibrary.widget.imageview.RoundImageView;

/**
 * 模拟GridLayout根据数据源切换布局，最多5种布局
 * @author xiao
 */
public class GridLayoutUtil {
    private static final int TYPE_SINGLE = 1;
    private static final int TYPE_DOUBLE = 2;
    private static final int TYPE_TRIPLE = 3;
    private static final int TYPE_QUADRA = 4;
    private static final int TYPE_PENTA = 5;
    private static final int MAX_COUNT = 5;

    /**
     * 创建GridLayout布局
     * @param activity 上下文
     * @param list 数据源
     * @param gl GridLayout
     * @param totalHight GridLayout总高度
     * @param rowMagin 行之间的间距
     * @param columnMaigin 列之间的间距
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public static void createGridLayout(Activity activity, List<GridListBean> list, GridLayout gl, int totalHight, int rowMagin, int columnMaigin) {
        if (list == null || list.size() <= 0) {
            LogUtil.i("", "list is null or size<=0");
            return;
        }
        if (list.size()>MAX_COUNT){
            list = list.subList(0, MAX_COUNT);
        }
        HashMap<Integer, Map<Integer, GridLayout.LayoutParams>> gridViewCoordinate = getGridViewCoordinate(totalHight, rowMagin, columnMaigin);
        Map<Integer, GridLayout.LayoutParams> layoutParamsMap = gridViewCoordinate.get(list.size());
        if (layoutParamsMap == null || layoutParamsMap.size() <= 0) {
            LogUtil.i("", "layoutParamsMap is null or size<=0");
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            RelativeLayout relativeLayout = (RelativeLayout) activity.getLayoutInflater().inflate(R.layout.item_grid, null, false);
            GridListBean gridListBean = list.get(i);
            GridLayout.LayoutParams layoutParams = layoutParamsMap.get(i);
            relativeLayout.setLayoutParams(layoutParams);
            TextView tv_top = relativeLayout.findViewById(R.id.tv_top);
            tv_top.setText(gridListBean.getTitle());
            gl.addView(relativeLayout, layoutParams);
            relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });
        }
    }


    /**
     * 获取所有布局的GridLayout.LayoutParams
     * @param totalHight
     * @param rowMagin
     * @param columnMaigin
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public static HashMap<Integer, Map<Integer, GridLayout.LayoutParams>> getGridViewCoordinate(int totalHight, int rowMagin, int columnMaigin) {
        HashMap<Integer, Map<Integer, GridLayout.LayoutParams>> mapType = new HashMap<>();
        List<Integer> list = new ArrayList<>();
        list.add(TYPE_SINGLE);
        list.add(TYPE_DOUBLE);
        list.add(TYPE_TRIPLE);
        list.add(TYPE_QUADRA);
        list.add(TYPE_PENTA);
        for (int i = 0; i < list.size(); i++) {
            mapType.put(list.get(i), getMapParams(list.get(i), totalHight, rowMagin, columnMaigin));
        }
        return mapType;
    }

    /**
     * 获取某个布局的GridLayout.LayoutParams
     * @param totalHight
     * @param rowMagin
     * @param columnMaigin
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public static HashMap<Integer, GridLayout.LayoutParams> getMapParams(int type, int totalHight, int rowMagin, int columnMaigin) {
        HashMap<Integer, GridLayout.LayoutParams> mapParams = new HashMap<>();
        switch (type) {
            case TYPE_SINGLE:
                for (int i = 0; i < TYPE_SINGLE; i++) {
                    GridLayout.LayoutParams params = new GridLayout.LayoutParams(GridLayout.spec(0, 2, 2f), GridLayout.spec(0, 3, 3f));
                    params.width = 0;
                    params.height = totalHight;
                    mapParams.put(i, params);
                }
                break;
            case TYPE_DOUBLE:
                for (int i = 0; i < TYPE_DOUBLE; i++) {
                    GridLayout.LayoutParams params = new GridLayout.LayoutParams();
                    params.width = 0;
                    params.height = totalHight;
                    if (i == 0) {
                        // 设置行列下标， 所占行列  ，比重
                        // 对应： layout_row  , layout_rowSpan , layout_rowWeight
                        params.rowSpec = GridLayout.spec(0, 2, 2f);
                        params.columnSpec = GridLayout.spec(0, 1, 1.5f);
                        params.setMargins(0, 0, columnMaigin, 0);
                    } else {
                        // 设置行列下标， 所占行列  ，比重
                        // 对应： layout_row  , layout_rowSpan , layout_rowWeight
                        params.rowSpec = GridLayout.spec(0, 2, 2f);
                        params.columnSpec = GridLayout.spec(1, 1, 1.5f);
                    }
                    mapParams.put(i, params);
                }
                break;
            case TYPE_TRIPLE:
                for (int i = 0; i < TYPE_TRIPLE; i++) {
                    GridLayout.LayoutParams params = getLayoutParams(totalHight);
                    if (i == 0) {
                        params.rowSpec = GridLayout.spec(0, 2, 1f);
                        params.columnSpec = GridLayout.spec(0, 1, 2f);
                        params.setMargins(0, 0, columnMaigin, 0);
                    } else if (i == 1) {
                        // 设置行列下标， 所占行列  ，比重
                        // 对应： layout_row  , layout_rowSpan , layout_rowWeight
                        params.rowSpec = GridLayout.spec(0, 1, 1f);
                        params.columnSpec = GridLayout.spec(1, 1, 1f);
                        params.setMargins(0, 0, 0, rowMagin);
                    } else {
                        params.rowSpec = GridLayout.spec(1, 1, 1f);
                        params.columnSpec = GridLayout.spec(1, 1, 1f);
                    }
                    mapParams.put(i, params);
                }
                break;
            case TYPE_QUADRA:
                for (int i = 0; i < TYPE_QUADRA; i++) {
                    GridLayout.LayoutParams params = getLayoutParams(totalHight);
                    if (i == 0) {
                        // 设置行列下标， 所占行列  ，比重
                        // 对应： layout_row  , layout_rowSpan , layout_rowWeight
                        params.rowSpec = GridLayout.spec(0, 2, 1f);
                        params.columnSpec = GridLayout.spec(0, 1, 1f);
                        params.setMargins(0, 0, columnMaigin, 0);
                    } else if (i == 1) {
                        // 设置行列下标， 所占行列  ，比重
                        // 对应： layout_row  , layout_rowSpan , layout_rowWeight
                        params.rowSpec = GridLayout.spec(0, 2, 1f);
                        params.columnSpec = GridLayout.spec(1, 1, 1f);
                        params.setMargins(0, 0, columnMaigin, 0);
                    } else if (i == 2) {
                        params.rowSpec = GridLayout.spec(0, 1, 1f);
                        params.columnSpec = GridLayout.spec(2, 1, 1f);
                        params.setMargins(0, 0, 0, rowMagin);
                    } else {
                        params.rowSpec = GridLayout.spec(1, 1, 1f);
                        params.columnSpec = GridLayout.spec(2, 1, 1f);
                    }
                    mapParams.put(i, params);
                }
                break;
            case TYPE_PENTA:
                for (int i = 0; i < TYPE_PENTA; i++) {
                    GridLayout.LayoutParams params = getLayoutParams(totalHight);
                    if (i == 0) {
                        params.rowSpec = GridLayout.spec(0, 2, 1f);
                        params.columnSpec = GridLayout.spec(0, 1, 1f);
                        params.setMargins(0, 0, columnMaigin, 0);
                    } else if (i == 1) {
                        // 设置行列下标， 所占行列  ，比重
                        // 对应： layout_row  , layout_rowSpan , layout_rowWeight
                        params.rowSpec = GridLayout.spec(0, 1, 1f);
                        params.columnSpec = GridLayout.spec(1, 1, 1f);
                        params.setMargins(0, 0, columnMaigin, rowMagin);
                    } else if (i == 2) {
                        params.rowSpec = GridLayout.spec(1, 1, 1f);
                        params.columnSpec = GridLayout.spec(1, 1, 1f);
                        params.setMargins(0, 0, columnMaigin, 0);
                    } else if (i == 3) {
                        params.rowSpec = GridLayout.spec(0, 1, 1f);
                        params.columnSpec = GridLayout.spec(2, 1, 1f);
                        params.setMargins(0, 0, 0, rowMagin);
                    } else {
                        params.rowSpec = GridLayout.spec(1, 1, 1f);
                        params.columnSpec = GridLayout.spec(2, 1, 1f);
                    }
                    mapParams.put(i, params);
                }
                break;
            default:
                break;
        }
        return mapParams;
    }

    private static GridLayout.LayoutParams getLayoutParams(int totalHight) {
        GridLayout.LayoutParams params = new GridLayout.LayoutParams();
        params.width = 0;
        params.height = totalHight / 2;
        return params;
    }
//
//    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
//    public static void createGridLayout(BaseActivity activity, List<GridListBean> list, GridLayout gridLayout) {
//        if (list == null || list.size() == 0) {
//            LogUtil.i("", "data is null");
//            return;
//        }
//
//        switch (list.size()) {
//            case TYPE_SINGLE:
//                creatSingleView(activity, list, gridLayout, ScreenUtil.dp2px(activity, 145));
//                break;
//            case TYPE_DOUBLE:
//                creatDoubleView(activity, list, gridLayout, ScreenUtil.dp2px(activity, 145));
//                break;
//            case TYPE_TRIPLE:
//                createTripleView(activity, list, gridLayout, ScreenUtil.dp2px(activity, 145));
//                break;
//            case TYPE_QUADRA:
//                creatQuadraView(activity, list, gridLayout, ScreenUtil.dp2px(activity, 145));
//                break;
//            case TYPE_PENTA:
//                createPentaView(activity, list, gridLayout, ScreenUtil.dp2px(activity, 145));
//                break;
//            default:
//                break;
//        }
//
//
//    }


//    /**
//     * 一个布局
//     *
//     * @param activity
//     * @param gl
//     * @param totalHight 布局总高度
//     *                   //     * @param rowMargin     两行之间的magin （totalHight-rowMargin）/2就是布局的高度
//     */
//    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
//    private static RelativeLayout creatSingleView(Activity activity, List<GridListBean> list, GridLayout gl, float totalHight) {
//        RelativeLayout relativeLayout = (RelativeLayout) activity.getLayoutInflater().inflate(R.layout.item_grid, null, false);
////        gl 2行3列
//// 设置行列下标， 所占行列  ，比重
//        // 对应： layout_row  , layout_rowSpan , layout_rowWeight
//        GridLayout.LayoutParams params = new GridLayout.LayoutParams(GridLayout.spec(0, 2, 2f), GridLayout.spec(0, 3, 3f));
//        params.width = 0;
//        params.height = (int) totalHight;
//        gl.addView(relativeLayout, params);
//        return relativeLayout;
//    }
//
//    /**
//     * 两个布局
//     *
//     * @param activity
//     * @param gl
//     * @param totalHight 布局总高度
//     */
//    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
//    private static void creatDoubleView(Activity activity, List<GridListBean> list, GridLayout gl, float totalHight) {
//        for (int i = 0; i < list.size(); i++) {
//            RelativeLayout relativeLayout = (RelativeLayout) activity.getLayoutInflater().inflate(R.layout.item_grid, null, false);
//            GridLayout.LayoutParams params = new GridLayout.LayoutParams();
////        gl 2行3列
//            params.width = 0;
//            params.height = (int) totalHight;
//            if (i == 0) {
//                // 设置行列下标， 所占行列  ，比重
//                // 对应： layout_row  , layout_rowSpan , layout_rowWeight
//                params.rowSpec = GridLayout.spec(0, 2, 2f);
//                params.columnSpec = GridLayout.spec(0, 1, 1.5f);
//                params.setMargins(0, 0, ScreenUtil.dp2px(activity, 9), 0);
//                relativeLayout.setLayoutParams(params);
//            } else {
//                // 设置行列下标， 所占行列  ，比重
//                // 对应： layout_row  , layout_rowSpan , layout_rowWeight
//                params.rowSpec = GridLayout.spec(0, 2, 2f);
//                params.columnSpec = GridLayout.spec(1, 1, 1.5f);
//            }
//
////        relativeLayout.setGravity(Gravity.);
//            gl.addView(relativeLayout, params);
//        }
//    }
//
//    /**
//     * 3个布局
//     *
//     * @param activity
//     * @param gl
//     * @param totalHight 布局总高度
//     */
//    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
//    private static void createTripleView(Activity activity, List<GridListBean> list, GridLayout gl, float totalHight) {
//        for (int i = 0; i < list.size(); i++) {
//            RelativeLayout relativeLayout = (RelativeLayout) activity.getLayoutInflater().inflate(R.layout.item_grid, null, false);
//            GridLayout.LayoutParams params = new GridLayout.LayoutParams();
////        gl 2行3列
//            params.width = 0;
//            params.height = (int) totalHight / 2;
//            if (i == 0) {
//                // 设置行列下标， 所占行列  ，比重
//                // 对应： layout_row  , layout_rowSpan , layout_rowWeight
//                params.rowSpec = GridLayout.spec(0, 2, 1f);
//                params.columnSpec = GridLayout.spec(0, 1, 2f);
//                params.setMargins(0, 0, ScreenUtil.dp2px(activity, 9), 0);
//                relativeLayout.setLayoutParams(params);
//            } else if (i == 1) {
//                // 设置行列下标， 所占行列  ，比重
//                // 对应： layout_row  , layout_rowSpan , layout_rowWeight
//                params.rowSpec = GridLayout.spec(0, 1, 1f);
//                params.columnSpec = GridLayout.spec(1, 1, 1f);
//                params.setMargins(0, 0, 0, ScreenUtil.dp2px(activity, 9));
//                relativeLayout.setLayoutParams(params);
//            } else {
//                params.rowSpec = GridLayout.spec(1, 1, 1f);
//                params.columnSpec = GridLayout.spec(1, 1, 1f);
//            }
//
////        relativeLayout.setGravity(Gravity.);
//            gl.addView(relativeLayout, params);
//        }
//    }
//
//    /**
//     * 四个布局
//     *
//     * @param activity
//     * @param gl
//     * @param totalHight 布局总高度
//     */
//    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
//    private static void creatQuadraView(Activity activity, List<GridListBean> list, GridLayout gl, float totalHight) {
//        for (int i = 0; i < list.size(); i++) {
//            RelativeLayout relativeLayout = (RelativeLayout) activity.getLayoutInflater().inflate(R.layout.item_grid, null, false);
//            GridLayout.LayoutParams params = new GridLayout.LayoutParams();
////        gl 2行3列
//            params.width = 0;
//            params.height = (int) totalHight / 2;
//            if (i == 0) {
//                // 设置行列下标， 所占行列  ，比重
//                // 对应： layout_row  , layout_rowSpan , layout_rowWeight
//                params.rowSpec = GridLayout.spec(0, 2, 1f);
//                params.columnSpec = GridLayout.spec(0, 1, 1f);
//                params.setMargins(0, 0, ScreenUtil.dp2px(activity, 9), 0);
//                relativeLayout.setLayoutParams(params);
//            } else if (i == 1) {
//                // 设置行列下标， 所占行列  ，比重
//                // 对应： layout_row  , layout_rowSpan , layout_rowWeight
//                params.rowSpec = GridLayout.spec(0, 2, 1f);
//                params.columnSpec = GridLayout.spec(1, 1, 1f);
//                params.setMargins(0, 0, ScreenUtil.dp2px(activity, 9), 0);
//                relativeLayout.setLayoutParams(params);
//            } else if (i == 2) {
//                params.rowSpec = GridLayout.spec(0, 1, 1f);
//                params.columnSpec = GridLayout.spec(2, 1, 1f);
//                params.setMargins(0, 0, 0, ScreenUtil.dp2px(activity, 9));
//                relativeLayout.setLayoutParams(params);
//            } else {
//                params.rowSpec = GridLayout.spec(1, 1, 1f);
//                params.columnSpec = GridLayout.spec(2, 1, 1f);
//            }
//
////        relativeLayout.setGravity(Gravity.);
//            gl.addView(relativeLayout, params);
//        }
//    }
//
//    /**
//     * 5个布局
//     *
//     * @param activity
//     * @param gl
//     * @param totalHight 布局总高度
//     */
//    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
//    private static void createPentaView(Activity activity, List<GridListBean> list, GridLayout gl, float totalHight) {
//        for (int i = 0; i < list.size(); i++) {
//            RelativeLayout relativeLayout = (RelativeLayout) activity.getLayoutInflater().inflate(R.layout.item_grid, null, false);
//            GridLayout.LayoutParams params = new GridLayout.LayoutParams();
////        gl 2行3列
//            params.width = 0;
//            params.height = (int) totalHight / 2;
//            if (i == 0) {
//                // 设置行列下标， 所占行列  ，比重
//                // 对应： layout_row  , layout_rowSpan , layout_rowWeight
//                params.rowSpec = GridLayout.spec(0, 2, 1f);
//                params.columnSpec = GridLayout.spec(0, 1, 1f);
//                params.setMargins(0, 0, ScreenUtil.dp2px(activity, 9), 0);
//                relativeLayout.setLayoutParams(params);
//            } else if (i == 1) {
//                // 设置行列下标， 所占行列  ，比重
//                // 对应： layout_row  , layout_rowSpan , layout_rowWeight
//                params.rowSpec = GridLayout.spec(0, 1, 1f);
//                params.columnSpec = GridLayout.spec(1, 1, 1f);
//                params.setMargins(0, 0, ScreenUtil.dp2px(activity, 9), ScreenUtil.dp2px(activity, 9));
//                relativeLayout.setLayoutParams(params);
//            } else if (i == 2) {
//                params.rowSpec = GridLayout.spec(1, 1, 1f);
//                params.columnSpec = GridLayout.spec(1, 1, 1f);
//                params.setMargins(0, 0, ScreenUtil.dp2px(activity, 9), 0);
//                relativeLayout.setLayoutParams(params);
//            } else if (i == 3) {
//                params.rowSpec = GridLayout.spec(0, 1, 1f);
//                params.columnSpec = GridLayout.spec(2, 1, 1f);
//                params.setMargins(0, 0, 0, ScreenUtil.dp2px(activity, 9));
//                relativeLayout.setLayoutParams(params);
//            } else {
//                params.rowSpec = GridLayout.spec(1, 1, 1f);
//                params.columnSpec = GridLayout.spec(2, 1, 1f);
//            }
//
////        relativeLayout.setGravity(Gravity.);
//            gl.addView(relativeLayout, params);
//        }
//    }

}
